package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Expenditure;
import cn.wolfcode.crm.query.ExpenditureQueryObject;
import cn.wolfcode.crm.service.IExpenditureService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("expenditure")
public class ExpenditureController {

    @Autowired
    private IExpenditureService expenditureService;

    @RequestMapping("")
    public String index(){
        return "expenditure/list";
    }

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ExpenditureQueryObject qo){
        return expenditureService.query(qo);
    }

    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Expenditure entity){
        JsonResult result = new JsonResult();
        try {
            expenditureService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Expenditure entity){

        JsonResult result = new JsonResult();
        try {
            expenditureService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("编辑操作失败");
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        JsonResult result = new JsonResult();
        try {
            expenditureService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return result;
    }
}
